#pragma once

#include <thread>

namespace cybertron {

class QuitableThread
{
public:
	QuitableThread() :_quit_flag(false), _is_running(false) {};
	virtual ~QuitableThread() {};

	void start() {
		if (!_is_running) {
			_thread = std::thread(&QuitableThread::run, this);
			_is_running = true;
		}
	}
	void stop() {
		_quit_flag = true;
		if (_is_running) {
			_thread.join();
			_is_running = false;
		}
	}

protected:
	bool _quit_flag;


private:


	void run() {
		while (!_quit_flag) {
			run_once();
		}
	}

	virtual void run_once() = 0;
	std::thread _thread;
	bool _is_running;
};
}